/** 变更详情 */
import { useContext, useEffect, useMemo, useState } from 'react';
import ScrollBarLayout from '@/components/ScrollBarLayout';
import Cls from 'classnames';
import { ConractControllerContext } from '../common';
import { ContractManageAPI } from '../common/type';
import { DetailInfoViewBottomBtn } from './DetailInfoViewBottomBtn';
import { DescriptionsType } from '@/components/DescriptionsType';
import { effectMainContract, getModifyUpdateDes, ntkoQueryParams } from '../mconfig';
import { wordHandChildParam } from '@/utils';
import { NtkoPlugin } from '@/common/NtkoPluginType';

export const DetailInfoViewTabsModify: React.FC<ContractManageAPI.IDetailInfoViewTabsModify> = ({ onOpenReadOfdWord }) => {
  /** 获取上下文 */
  const { curRes, styleFixCls, contractDetailInfo, oldContractInfo, contractForm, contractDispatch, contarctJsonList } = useContext(ConractControllerContext);

  /** 变更事项 */
  const [changeModifyItem, setChangeModifyItem] = useState<Record<string, any>>({
    changeSum: 0, //是否变更合同金额
    changeTerm: 0, //是否变更履约期限
    changePay: 0, //是否变更支付方式
    changeSubject: 0, //是否变更合同主体
    changeOpposite: 0, //是否变更相对方
    changeDetail: 0, //是否变更货物清单
    isPayDisabled: false, // 支付方式是否可修改
    changeOther: 0, //是否变更其他
  });

  /** 编辑初始化变更信息 */
  useEffect(() => {
    if (contractDetailInfo && contractDetailInfo?.id) {
      const modifyItem: Record<string, any> = {
        changeSum: contractDetailInfo?.changeSum ? contractDetailInfo?.changeSum : 0,
        changeTerm: contractDetailInfo?.changeTerm ? contractDetailInfo?.changeTerm : 0,
        changePay: contractDetailInfo?.changePay ? contractDetailInfo?.changePay : 0,
        changeSubject: contractDetailInfo?.changeSubject ? contractDetailInfo?.changeSubject : 0,
        changeOpposite: contractDetailInfo?.changeOpposite ? contractDetailInfo?.changeOpposite : 0,
        changeDetail: contractDetailInfo?.changeDetail ? contractDetailInfo?.changeDetail : 0,
        changeOther: contractDetailInfo?.changeOther ? contractDetailInfo?.changeOther : 0,
      };
      setChangeModifyItem(modifyItem);
    }
  }, [contractDetailInfo]);

  /** 引入在线预览 */
  const ntkoBrowser = useMemo(() => {
    const { ntkoBrowser } = NtkoPlugin(() => {});
    return ntkoBrowser;
  }, []);

  /** 点击在线预览文件 */
  const onNtkoReadWord = (urlName: string, urlObj: Record<string, any>, onlyRead?: boolean) => {
    const { ntkoWordUrl } = ntkoQueryParams(urlObj, curRes, urlName, onlyRead);

    const childStr = wordHandChildParam(urlObj);

    const extensBool = ntkoBrowser.ExtensionInstalled();
    if (extensBool) {
      ntkoBrowser?.openWindow(ntkoWordUrl, false, '', childStr);
    }
  };

  /** 初始化原数据 */
  effectMainContract(oldContractInfo, contractForm, changeModifyItem, setChangeModifyItem, contractDispatch, contarctJsonList, contractDetailInfo);

  /** 获取表格配置 */
  const { config } = getModifyUpdateDes({
    openOldContractInfo: () => {},
    changeModifyItem,
    setChangeModifyItem,
    setOpenSujbectModal: () => {},
    openCounterpartModal: () => {},
    onNtkoReadWord,
    onOpenReadOfdWord,
    setOpenMaterialModal: () => {},
  });
  return (
    <>
      <div className="commonTabsBodyDom">
        <div className={Cls('tabsBodyInfoDom', `${styleFixCls}updateFormBox`)}>
          <ScrollBarLayout style={{ padding: '5px 20px' }}>
            <DescriptionsType config={config} />
          </ScrollBarLayout>
        </div>

        <DetailInfoViewBottomBtn position="bottom" />
      </div>
    </>
  );
};
